package com.usabb.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.HomeStayDO;
import com.usabb.service.HomeStayApiService;
import com.usabb.service.HomeStayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;

/**
 * 民宿
 */
@Slf4j
@RequestMapping("usabb/api/home-stay")
@Controller
public class HomeStayApiController extends BaseController {
    @Autowired
    private HomeStayService homeStayService;
    @Autowired
    private HomeStayApiService homeStayApiService;


    @CrossOrigin
    @ResponseBody
    @GetMapping("{id}")
    public Result<HomeStayDO> getHomeStay(@Valid @Pattern(regexp = "[0-9]+") @PathVariable(name = "id") Long id) {
        HomeStayDO homeStay = homeStayApiService.getFullDOById(id);
        return Result.ok(homeStay);
    }

    @CrossOrigin
    @GetMapping("list")
    @ResponseBody
    public Result<Page<HomeStayDO>> getList(
            @RequestParam(name = "countOfRoom", required = false) String countOfRoom,
            @RequestParam(name = "cityIn", required = false) Integer cityIn,
            @RequestParam(name = "hireFee", required = false) BigDecimal hireFee) {
        //获取请求参数
        log.info("input params: countOfRoom: {}, cityIn:{}, hireFee:{} ", countOfRoom, cityIn, hireFee);

        HomeStayDO homeStayParam = new HomeStayDO();
        homeStayParam.setCityIn(cityIn);
        homeStayParam.setCountOfRoom(countOfRoom);
        homeStayParam.setHireFee(hireFee);
        Wrapper<HomeStayDO> wrapper = new EntityWrapper<>(homeStayParam).orderBy("id", false);

        Page<HomeStayDO> page = homeStayService.selectPage(getPage(HomeStayDO.class), wrapper);
        page.getRecords().forEach(it -> homeStayApiService.appendDetail(it));

        return Result.ok(page);
    }

}
